package com.compmov.socialnetwork;

import com.compmov.datamodel.Comentario;
import com.compmov.datamodel.Persona;
import com.compmov.datamodel.Lugar;
import com.compmov.webservice.ComentarioWSClient;
import com.compmov.webservice.WSClientException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddComments extends Activity {

	private Spinner ranking;
	private EditText comment;
	private String id;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_comments);
		Bundle extras = getIntent().getExtras();
	    id = extras.getString("id_lugar");
	    
	     
	   
	        
		this.ranking =(Spinner) findViewById(R.id.spinnercomment);
		this.comment =(EditText) findViewById(R.id.comment);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.numero_ranking2, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    ranking.setAdapter(adapter);
	}
	
	public void add(View v){
		if(comment.getText().length()==0){
			Toast notificacionToast = Toast.makeText(getApplicationContext(),
					"Indique un comentario", Toast.LENGTH_SHORT);
			notificacionToast.show();
			return;
		}
		Persona per=com.compmov.libraries.Utils.getPersona();
		String fecha=com.compmov.libraries.Utils.obtenFecha();
		Lugar lug=new Lugar();
		 lug.setId(Integer.valueOf(id));
		Comentario com= new Comentario(0,comment.getText().toString(), 5-ranking.getSelectedItemPosition(),
				fecha,per,lug);
		ComentarioWSClient comentWS=new ComentarioWSClient("comentario");
		try{
			comentWS.addComentario(com);
			Toast notificacionToast = Toast.makeText(getApplicationContext(),
					"Registro Exitoso", Toast.LENGTH_SHORT);
			notificacionToast.show();
			finish();
		} catch (WSClientException e) {
			Toast.makeText(getBaseContext(),"Error en el servidor. Intente de nuevo",
					Toast.LENGTH_SHORT).show();
			return;
		}
	}

	public void cancel(View v){
		finish();		
	}
		
	
}
